#include "NahidaUnitTest.h"

std::vector<NahidaProject::NahidaUnitTest::TestCase>& NahidaProject::NahidaUnitTest::GetTests() {
	static std::vector<TestCase> tests;
	return tests;
}

void NahidaProject::NahidaUnitTest::RegisterTest(const std::string& testName, std::function<void()> testFunc){
	GetTests().push_back({ testName, testFunc });
}

void NahidaProject::NahidaUnitTest::RunAllTests(){
	int passed = 0;
	int failed = 0;

	for (const auto& test : GetTests()) {
		try {
			test.func();
			std::cout << "[SUCCESS] Task Slot: " << test.name << std::endl;
			++passed;
		}
		catch (const std::exception& ex) {
			std::cerr << "[FAILURE] Task Slot: " << test.name << " - " << ex.what() << std::endl;
			++failed;
		}
	}

	std::cout << "===================================" << std::endl;
	std::cout << "Total: " <<  (passed + failed) << ", Passed: " << passed << ", Failed: " << failed << std::endl;
	std::cout << "===================================" << std::endl;
}

void NahidaProject::NahidaUnitTest::RunTestByName(const std::string& testName){
	for (const auto& test : GetTests()) {
		if (test.name == testName) {
			try {
				test.func();
				std::cout << "[SUCCESS]  Task Slot: " << test.name << std::endl;
			}
			catch (const std::exception& ex) {
				std::cerr << "[FAILURE] Task Slot: "  << test.name << " - " << ex.what() << std::endl;
			}
			return;
		}
	}
	std::cerr << "Test '" << testName << "' not found." << std::endl;
}

float NahidaProject::NahidaUnitTest::CalculateExecuteTime(std::function<void()> function) {
	float start = ::clock();
	function();
	float end = ::clock();
	return end - start;
}
